//
//  Tool.m
//  cascade-realm
//
//  Created by QSC on 16/5/24.
//

#import "Tool.h"
#import "BQActivityView.h"
#import <AFNetworking/AFNetworking.h>
@implementation Tool
/**
 *colors 渐变的颜色 @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor, (__bridge id)[UIColor blueColor].CGColor];
 *locations 渐变颜色的分割点 @[@0.3, @0.5, @1.0];
 *startPoint&endPoint 颜色渐变的方向，范围在(0,0)与(1.0,1.0)之间，如(0,0)(1.0,0)代表水平方向渐变,(0,0)(0,1.0)代表竖直方向渐变
 */
+ (void)colorGradientLayerForView:(UIView *)view frame:(CGRect)frame colors:(NSArray *)colors locations:(NSArray *)locations{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    
    NSMutableArray *colorsArr = [NSMutableArray array];
    for (UIColor *color in colors) {
        [colorsArr addObject:(__bridge id)[color CGColor]];
    }
    gradientLayer.colors = colorsArr;
    gradientLayer.locations = locations;
    
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = frame;

    [view.layer insertSublayer:gradientLayer atIndex:0];
}

+ (void)colorGradientForView:(UIView *)view frame:(CGRect)frame colors:(NSArray *)colors locations:(NSArray *)locations
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    
    NSMutableArray *colorsArr = [NSMutableArray array];
    for (UIColor *color in colors) {
        [colorsArr addObject:(__bridge id)[color CGColor]];
    }
    gradientLayer.colors = colorsArr;
    gradientLayer.locations = locations;
    
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1.0);
    gradientLayer.frame = frame;
    [view.layer insertSublayer:gradientLayer atIndex:0];
}


+ (UIImage *)getBGImage:(UIColor *)startColor and:(UIColor *)endColor directionType:(NSInteger)directionType option:(CGSize)size
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)startColor.CGColor,
                             (__bridge id)endColor.CGColor];
    gradientLayer.locations = @[@(0.0f), @(1.0f)];
    
    if (directionType == 1) { //AC - BD
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
    }else if (directionType == 2){//AB - CD
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(0, 1);
    }else if (directionType == 3){//A - D
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 1);
    }else if (directionType == 4){//C - B
        gradientLayer.startPoint = CGPointMake(0, 1);
        gradientLayer.endPoint = CGPointMake(1, 0);
    }
    
    
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, NO, 0);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return gradientImage;
}

+ (void)ShowLoading{
   
    [BQActivityView showActiviTy];
}
+ (void)HideLoading{
    
    [BQActivityView hideActiviTy];
}




+ (void)moreClick:(NSString *)userId{
    
    NSMutableArray * arr = [NSMutableArray arrayWithArray:@[@"Block",@"Report"]];
    if (![CAUserInfo.shared.followIds containsObject:userId]) {
        [arr insertObject:@"Follow" atIndex:0];
    }

    [CAAlertView ShowAlertActionSheet:arr actionBlock:^(NSString * _Nonnull action) {
        
        if ([action isEqualToString: @"Follow"]) {
            [self Follow:userId];
        }else if ([action isEqualToString: @"Block"]){
            [self Block:userId];
        }else{
            [self Report:userId];
        }
    }];
}

+ (void)Follow:(NSString *)userId{
    
    if ([CAUserInfo.shared.followIds containsObject:userId]) {
        return;
    }
    
    NSDictionary * dic = @{
        @"followUserId":userId,
    };
    
    [Http postWithURL:userAddFriend params:dic success:^(id  _Nonnull responseObject) {
            
        [EasyTextView showText:@"follow success"];
        [CAUserInfo.shared.followIds addObject:userId];
        [[NSNotificationCenter defaultCenter] postNotificationName:FollowDidChange object:nil];
    
    } failure:^(id  _Nonnull errObject) {
        
    }];
}

+ (void)Block:(NSString *)userId{
    
    
    NSDictionary * dic = @{
        @"broadcasterId":userId,
        @"complainCategory":@"Block"
    };
    [Http PostLogWithPage:@"block"];
    [Http postWithURL:insertRecord params:dic success:^(id  _Nonnull responseObject) {
        [EasyTextView showText:@"block success"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:BlockDidChange object:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UINavigationController * nav = (UINavigationController *) [UIApplication sharedApplication].keyWindow.rootViewController;
            [nav popViewControllerAnimated:YES];
        });
 
        
        if ([CAUserInfo.shared.followIds containsObject:userId]) {
            [Http postWithURL:userUnFriend params:@{@"followUserId":userId} success:^(id  _Nonnull responseObject) {
                    

                [CAUserInfo.shared.followIds removeObject:userId];
                [[NSNotificationCenter defaultCenter] postNotificationName:FollowDidChange object:nil];
                
           
                
            } failure:^(id  _Nonnull errObject) {
                
            }];
        }
        
        
        
        
    } failure:^(id  _Nonnull errObject) {
        
    }];
    
}

+ (void)Report:(NSString *)userId{
    
    
    [CAAlertView ShowAlertActionSheet:@[
        @"Pornographic",@"False gender",@"Fraud",@"Political sensitive",@"Other"
        ] actionBlock:^(NSString * _Nonnull action) {
        
       
        [self ReportRquest:action userId:userId];
        
    }];
}

+ (void)ReportRquest:(NSString *)type userId:(NSString *)userId{
    
    NSDictionary * dic = @{
        @"broadcasterId":userId,
        @"complainCategory":@"Report",
        @"complainSub":type
    };
    [Http PostLogWithPage:@"report"];
    [Http postWithURL:insertRecord params:dic success:^(id  _Nonnull responseObject) {
        [EasyTextView showText:@"report success"];
        
    } failure:^(id  _Nonnull errObject) {
        
    }];
}


//随机字母
+ (NSString *)randomStringWithNumber:(NSInteger)number{ //number 是需要的个数
    NSString *ramdom;
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 1; i ; i ++) {
    int a = (arc4random() % 122); //如需要可以改变数值大小  这儿的数值是ASCII值
    if (a > 96) { //这儿是小写字母 如需要自行更改
        char c = (char)a;
        [array addObject:[NSString stringWithFormat:@"%c",c]];
        if (array.count == number) {
            break;
        }
    } else continue;
    }
    ramdom = [array componentsJoinedByString:@""];//这个是把数组转换为字符串
    return ramdom;
}


+ (UIImage *)getImage:(NSString *)image{
    
    if ([image containsString:@"--image"]) {
        
        return  [[SDImageCache sharedImageCache] imageFromCacheForKey:image];
        
        
    }else{
        return [UIImage imageNamed:image];
    }
    
    
    
}
@end
